package com.sdk.pay.transfer.api;


import com.sdk.pay.transfer.api.params.DESParams;
import com.sdk.pay.transfer.api.params.Params;
import com.sdk.pay.transfer.listener.CallBack;
import com.sdk.pay.transfer.constant.PayContext;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;

/**
 * Created by Joe on 2017/10/10 16:24.
 */

public class PostQrCodeRemoteInfoAPI extends APISHelper{
    private static final String URL = "api/Task/get";
    private static final String TAG = "PostQrCodeRemoteInfoAPI";

    public void PostQrCodeRemoteInfo(final Listener listener) {
        try {
            post(TAG, URL, new CallBack() {
                @Override
                public void onSuccess(Call call, String response) throws IOException {
                    try {
                        JSONObject object = new JSONObject(response);
                        if (object.getString("flag").equals(PayContext.TASK_SUCCESS)) {
                            if (listener != null)
                                listener.onSuccess(object.getString("Task"));
                        } else {
                            if (listener != null)
                                listener.onFailure(-1, object.getString("msg"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        if (listener != null) {
                            listener.onFailure(-1, e.getMessage());
                        }
                    }
                }

                @Override
                public void onError(int code) {
                    if (listener != null)
                        listener.onFailure(code, "");
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
            if (listener != null)
                listener.onFailure(-1, e.getMessage());
        }
    }
    @Override
    protected Params initParams() throws JSONException {
        Params params = new DESParams();
        params.put("Task_Id", "");
        params.put("State", "0");
        params.put("Img_Type", "3");
        return params;
    }
    public interface Listener {
        void onSuccess(String message);
        void onFailure(int code, String message);
    }
}
